<template>
	<view>
		<!-- 答题组件 -->
		<kz-question v-if="questions" mode="TRAINING" :title="cateName" :questions="questions"
			:questionCount="questionCount" :pageCount="pageCount" :currentPage="currentPage" :viewMode="mode"
			@loadQuestion="getQuestion"></kz-question>

		<!-- toast提示 -->
		<tui-toast ref="toast"></tui-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cateId: 0,
				cateName: '',
				questions: [],
				questionCount: 0,
				pageCount: 100,
				currentPage: 0,
				lastPage: 0,
				mode: 'normal',

				//计时器
				time: '00:00:00',
				timer: null
			}
		},
		onLoad(e) {
			this.cateId = e.cateId
			this.cateName = e?.cateName
			this.mode = e.mode
			this.getQuestion()
		},
		onShow(){
			this.startTimer();
		},
		onHide(){
			this.stopTimer();
		},
		methods: {
			submitQuestion(paperData) {
				console.log('paperData', paperData)
			
				if (this.isSubmit) {
					return
				}
			
				uni.showLoading({
					title: '交卷中'
				})
			
				let data = {
					paper_id: this.paper.id,
					start_time: this.start_time,
					questions: paperData.questions,
					room_id: this.room_id,
					room_grade_id: this.room_grade_id,
				}
			
				this.http('paper/submit', data, 'post').then(res => {
					console.log('paper submit res', res)
					this.result = res
					this.score = res.score
					this.isSubmit = true
			
					// 积分提示
					let point = res.point
					if (point?.get_point) {
						this.$refs.toast.show({
							title: "积分+" + point.get_point,
							content: point.type,
							imgUrl: "/static/toast/info-circle.png",
							icon: true,
							duration: 4000,
						})
					}
			
				})
			},
			//计时器
			startTimer() {
				this.timer = setInterval(() => {
					let parts = this.time.split(':');
					let seconds = parseInt(parts[2], 10) + 1;

					if (seconds === 60) {
						parts[1] = (parseInt(parts[1], 10) + 1).toString().padStart(2, '0');
						seconds = 0;
					}
					parts[2] = seconds.toString().padStart(2, '0');

					if (parseInt(parts[1]) === 60) {
						parts[0] = (parseInt(parts[0], 10) + 1).toString().padStart(2, '0');
						parts[1] = '00';
					}

					this.time = parts.join(':');
				}, 1000);
			},
			stopTimer() {
				if (this.timer) {
					clearInterval(this.timer);
					this.timer = null;
					console.log(this.time)
				}
			},
			// 获取试题（包括延迟获取）
			getQuestion(page = 1, callback = null) {
				console.log('getQuestion', page, this.currentPage, this.lastPage)
				// 避免重复获取
				if (page <= this.currentPage || (this.lastPage && page > this.lastPage)) {
					return
				}

				// 请求参数
				let params = {
					page: page,
					page_count: this.pageCount,
					cate_id: this.cateId,
					mode: this.mode,
					type: 'train',
				}

				this.http('question/train', params).then(res => {
					if (res.code == 1) {
						// 默认分页方式
						if (this.mode == 'normal') {
							this.questionCount = res.data.total
							this.currentPage = res.data.current_page
							this.lastPage = res.data.last_page
							this.questions = this.questions.concat(res.data.data)
						} else {
							// 记忆、随机模式暂时只能获取全部题目了
							this.questions = res.data.data
						}

						// 积分提示
						let point = res.data.point
						console.log('point', point)
						if (point?.get_point > 0) {
							this.$refs.toast.show({
								title: "积分+" + point.get_point,
								content: point.type,
								imgUrl: "/static/toast/info-circle.png",
								icon: true,
								duration: 4000,
							})
						}
					} else {
						this.utils.alert(res.msg, () => {
							this.utils.goback()
						})
					}

					if (callback) {
						callback()
					}
				})
			}
		}
	}
</script>

<style>

</style>